package com.sunther.document.entity;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.util.List;

/**
 * @author csb
 * @description: TODO 体系详情表
 * @date 2023/5/17 16:16
 */
@TableName(value = "document_system_v1")
public class FileSystemEntity extends BaseEntity{

    @TableId
    private String id;

    private String parentId;

    private Integer level;

    private String name;

    private String midId;

    @TableField(exist = false)
    List<FileSystemEntity> children;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMidId() {
        return midId;
    }

    public void setMidId(String midId) {
        this.midId = midId;
    }

    public List<FileSystemEntity> getChildren() {
        return children;
    }

    public void setChildren(List<FileSystemEntity> children) {
        this.children = children;
    }


    /**
     * hutool 树结构工具
     * @param systems
     * @return
     */
    public static List<Tree<String>> getTrees(List<FileSystemEntity> systems) {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //默认为id
        treeNodeConfig.setIdKey("id");
        //默认为parentId
        treeNodeConfig.setParentIdKey("parentId");
        //排序字段
        treeNodeConfig.setWeightKey("level");
        // 最大递归深度 todo 最大多少
        treeNodeConfig.setDeep(10);
        //2.转为树结构,其中rootId参数代表根节点的父级id值
        List<Tree<String>> treeList = TreeUtil.build(systems, "0", treeNodeConfig,
                (treeNode, tree)-> {
                    //对key进行映射赋值
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getLevel());
                    tree.setName(treeNode.getName());
                });
        return treeList;
    }
}
